package com.qeado.admin.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.qeado.admin.dao.AdminAccountManageDao;
import com.qeado.admin.model.AdminAccount;
import com.qeado.admin.model.Menu;
import com.qeado.admin.model.Role;
import com.qeado.admin.service.AdminAccountManageService;

@Service
public class AdminAccountManageServiceImpl implements AdminAccountManageService {
	@Autowired
	private AdminAccountManageDao adminAccountManageDao;
	
	public AdminAccount validateUser(String loginName, String password) {
        return adminAccountManageDao.validateUser(loginName, password);
    }

    public List<Menu> getMenuListByUser(AdminAccount userInfo) {
    	List<Menu> mainMenuList = adminAccountManageDao.getMenuListByUser(userInfo);
    	if(mainMenuList != null && mainMenuList.size() > 0){
    		Map<String, String> map = new HashMap<String, String>();
			map.put("roleId", userInfo.getRoleId());
    		for(int i=0;i<mainMenuList.size();i++) {
    			map.put("menuId", mainMenuList.get(i).getId());
    			mainMenuList.get(i).setMenuList(adminAccountManageDao.getMenuListByMenuIdAndRoleId(map));
    		}
    	}
        return mainMenuList;
    }

    public List<AdminAccount> searchAdminAccount(AdminAccount account) {
        return adminAccountManageDao.searchAdminAccount(account);
    }

    public AdminAccount searchAdminAccountById(String id) {
        return adminAccountManageDao.searchAdminAccountById(id);
    }

    public String saveAdminAccount(AdminAccount account) {
        if(account.getId() != null && !"".equals(account.getId())) {
        	if(account.getOldpassword() != null && !"".equals(account.getOldpassword())) {
	        	Object obj = adminAccountManageDao.searchAccountRole(account);
	        	if(obj == null) {//obj == null say that this account is not admin role.
	        		obj = adminAccountManageDao.validateOldpassword(account);
	            	if(obj == null || "".equals(obj)) {
	            		return "旧密码错误，请检查！";
	            	}
	        	}
        	}
            adminAccountManageDao.updateAdminAccount(account);
        } else {
        	String id = adminAccountManageDao.searchAdminAccountByName(account);
        	if(id != null){
        		return "该用户已存在！";
        	}
            account.setId(UUID.randomUUID().toString());
            adminAccountManageDao.saveAdminAccount(account);
        }
        if(account.getRoleId() != null && !"".equals(account.getRoleId())){
        	Map<String, String> map = new HashMap<String, String>();
            map.put("id", UUID.randomUUID().toString());
            map.put("adminAccountId", account.getId());
            map.put("creator", account.getCreator());
            map.put("roleId", account.getRoleId());
            adminAccountManageDao.deleteAdminAccountRole(map);
            adminAccountManageDao.saveAdminAccountRole(map);
        }
        return null;
    }

    public void deleteAdminAccount(String id) {
        adminAccountManageDao.deleteAdminAccount(id);
    }
    
	public List<Role> searchRoleList() {
		return adminAccountManageDao.searchRoleList();
	}

	public AdminAccount login(String loginName, String password) {
		return adminAccountManageDao.validateUser(loginName, password);
	}

    
}
